package regionalFoods

import (
	"github.com/gin-gonic/gin"
	"savor_gin/controllers"
	. "savor_gin/errorType"
	"savor_gin/models"
	"strconv"
)

// 上传地域美食
func AddRegionalFood(c *gin.Context) {
	var regionalFood models.RegionalFoods
	if err := c.ShouldBindJSON(&regionalFood); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	if err := regionalFood.Create(); err != nil {
		controllers.SendResp(c, DataBaseError, nil)
		return
	}
	controllers.SendResp(c, OK, regionalFood)
}

// 更新地域美食信息
func UpdateRegionalFoodInfo(c *gin.Context) {
	foodId, err := strconv.Atoi(c.Param("foodId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.RegionalFoods{RegionalFoodID: uint64(foodId)}
	if err := c.ShouldBind(s); err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if err := s.Update(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, s)
}

// 删除地域美食
func DeleteRegionalFood(c *gin.Context) {
	foodId, err := strconv.Atoi(c.Query("foodId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.RegionalFoods{RegionalFoodID: uint64(foodId)}
	if err := s.Delete(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, nil)
}

// 根据id获取地域美食信息
func GetRegionalFoodById(c *gin.Context) {
	foodId, err := strconv.Atoi(c.Query("foodId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetRegionalFoodsById(uint64(foodId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}

// 查询RecipeId对应的地域美食
func GetRegionalFoodListByRecipeId(c *gin.Context) {
	regionalFoodId, err := strconv.Atoi(c.Query("recipeId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetRegionByRecipeId(uint64(regionalFoodId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}

// 模糊查询地域美食列表
func GetRegionalFoodListLikeKey(c *gin.Context) {
	keyword := c.Query("keyword")
	if keyword == "" {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	if s, err := models.GetRegionByKey(keyword); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}

// 查询所有的地域美食类别
func GetRegionalFoodList(c *gin.Context) {
	if s, err := models.GetRegion(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}
